package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PrinterSetting;

public class PrinterSettingService {
	private DatabaseHelper DBHelper;
	private Context mContext;
	private String errorString;

	public PrinterSettingService() {

	}

	public PrinterSettingService(Context context) {
		// this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(
				GlobalInfo.getInstance().ShareContext);
	}

	public List<PrinterSetting> FindAll() {
		List<PrinterSetting> obj = new ArrayList<PrinterSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.PrinterSetting.SCHEMA_NAME, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ps_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_ID));
			String ps_name = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_NAME));
			String ps_ip = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_IP));
			int ps_port = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_PORT));

			PrinterSetting pr = new PrinterSetting(ps_id, ps_name, ps_ip, ps_port);
			pr.setType("O");
			obj.add(pr);
		}
		cursor.close();
		return obj;
	}
	
	public PrinterSetting FindByID(int no){
		PrinterSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.PrinterSetting.SCHEMA_NAME +" where ps_id = "+ no, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ps_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_ID));
			String ps_name = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_NAME));
			String ps_ip = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_IP));
			int ps_port = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_PORT));

			obj = new PrinterSetting(ps_id, ps_name, ps_ip, ps_port);
		}
		cursor.close();
		return obj;
	}
	
	public PrinterSetting FindByName(String name){
		PrinterSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.PrinterSetting.SCHEMA_NAME +" where ps_name = '"+ name + "'", null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ps_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_ID));
			String ps_name = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_NAME));
			String ps_ip = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_IP));
			int ps_port = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_PORT));

			obj = new PrinterSetting(ps_id, ps_name, ps_ip, ps_port);
		}
		cursor.close();
		return obj;
	}
	
	public String getPrinterIPByID(int id){
		String ip = "";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select ps_ip from " + DatabaseSchema.PrinterSetting.SCHEMA_NAME +" where ps_id = "+ id, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return ip;
		}
		while (cursor.moveToNext()) {
			ip = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PrinterSetting.PS_IP));
		}
		cursor.close();
		return ip;
	}
	
	public boolean CheckPrinter(int printer_id) {
		boolean lb_check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select ps_id from "+ DatabaseSchema.PrinterSetting.SCHEMA_NAME +" where ps_id  = " + printer_id;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {
			lb_check = false;
		}
		cursor.close();
		return lb_check;
	}

	public boolean Insert(PrinterSetting obj) {
		boolean check = true;
		// SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "INSERT INTO t_printer_setting" + "("
				+ DatabaseSchema.PrinterSetting.PS_NAME + ","
				+ DatabaseSchema.PrinterSetting.PS_IP + "," 
				+ DatabaseSchema.PrinterSetting.PS_PORT + ") values ('"
				+ obj.getPs_name() + "','" + obj.getPs_ip() + "'," 
				+ obj.getPs_port() + ")";
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}

	public boolean Update(PrinterSetting obj) {
		boolean check = true;
		// SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Update t_printer_setting set "
				+ DatabaseSchema.PrinterSetting.PS_NAME + " = '"
				+ obj.getPs_name() + "', "
				+ DatabaseSchema.PrinterSetting.PS_IP + " = '" + obj.getPs_ip() + "'," 
				+ DatabaseSchema.PrinterSetting.PS_PORT + " = " + obj.getPs_port() 
				+ " where " +  DatabaseSchema.PrinterSetting.PS_ID + " = " + obj.getPs_id();
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean Delete(int ps_id) {
		boolean check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Delete from t_printer_setting ";
		String ls_where = "where " +  DatabaseSchema.PrinterSetting.PS_ID + " = " + ps_id;
		sql = sql + ls_where;
		try {
			db.execSQL(sql);
		} catch (Exception e) {
			check = false;
		}
		return check;
	}
	
	public int getMaxPrintNo(){
		int order = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select Max(ps_id) from " + DatabaseSchema.PrinterSetting.SCHEMA_NAME, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return order;
		}
		if (cursor.moveToNext()) {
			order = cursor.getInt(0);
		}
		cursor.close();
		return order;
	}
	
	public boolean ItemHavePrinter(int printerID){
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = DBHelper.rawQuery("select F01 from t_item_setting where printer_id = " + printerID, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return result;
		}
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		return result;
	}
}
